package com.example.administrator.hprogressbar;

import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    private NumberProgressView np_numberProgressBar;
    private Button btn_numberProgressBar;
    private int progress;

    protected static final int WHAT_INCREASE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        np_numberProgressBar = (NumberProgressView) findViewById(R.id.np_numberProgressBar);
        btn_numberProgressBar = (Button) findViewById(R.id.btn_numberProgressBar);
        btn_numberProgressBar.setBackgroundColor(0xffff0077);

        btn_numberProgressBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                increase();
            }


        });

    }

    private Handler handler  = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            progress ++;
            np_numberProgressBar.setProgress(progress);
            handler.sendEmptyMessageDelayed(WHAT_INCREASE,getRadomNumber(50,200));
            if (progress>=100){
                handler.removeMessages(WHAT_INCREASE);
            }

            super.handleMessage(msg);
        }
    };

    private void increase() {
        progress = 0;
        np_numberProgressBar.setProgress(0);
        handler.removeMessages(WHAT_INCREASE);
        handler.sendEmptyMessage(WHAT_INCREASE);

    }

    /*
    * 得到两个整数之间的一个随机数
    *
    * start  较小的数
    *
    * end  较大的数
    * */

    public int getRadomNumber(int start, int end){
        return  (int)(start + Math.random()*(end-start));
    }
}
